<template>
	<view class="page" :style="{ 'min-height': h + 'px', 'padding-top': mt + 'px' }">
		<c-nav-bar :title="title"></c-nav-bar>
		<!-- <view class="scan_add box" @tap="toScan">
			<image src="@/static/my/commonInformation/icon_camera.png"></image>
			<view>
				<text>扫描证件添加</text>
				<text>仅支持二代身份证</text>
			</view>
		</view> -->
		<view class="ck box">
			<!-- <view class="item" @tap="passengerShow = true">
				<view>乘客类型</view>
				<view>{{passenger}}</view>
				<view>
					<image src="@/static/my/commonInformation/icon_yjt.png"></image>
				</view>
			</view>
			<view class="hr"></view> -->
			<view class="item">
				<view>姓名</view>
				<view>
					<input type="text" placeholder="与乘客证件姓名一致" v-model="passengerNameCopy" @blur="nameSure" @confirm="nameSure">
				</view>
			</view>
		</view>
		<view class="ck box">
			<view class="item" @tap="credentialsShow = true">
				<view>证件类型</view>
				<view>{{ credentials }}</view>
				<view>
					<image src="@/static/my/commonInformation/icon_yjt.png"></image>
				</view>
			</view>
			<view class="hr"></view>
			<view class="item">
				<view>证件号码</view>
				<view>
					<input type="text" placeholder="请输入乘客证件号码" v-model="idcardCopy" @blur="idcardSure" @confirm="idcardSure">
				</view>
			</view>
			<block v-if="!isIdCard">
				<view class="hr"></view>
				<view class="item">
					<view>年龄</view>
					<view>
						<input type="text" placeholder="请输入乘客年龄" v-model="age" @blur="setAge" @confirm="setAge">
					</view>
				</view>
			</block>
		</view>
		<!-- <view class="yhzc box" v-if="show">
			<view class="item">
				<view>证件类型</view>
				<view>
					<text>本地居民</text>
					<text>点击了解详情</text>
				</view>
			</view>
			<view class="tickets">
				<view v-for="(item,index) in tickets" :key="index" :class="current===index?'active':''" @tap="current=index">
					<text>{{item.title}}</text>
					<text v-for="(ite,idx) in item.conditions" :key="idx">{{ite}}</text>
				</view>
			</view>
		</view> -->

		<view class="to_add">
			<view class="sure" @tap="sure">确定</view>
		</view>

		<u-picker :itemHeight="88" :immediateChange="true" :show="passengerShow" :columns="passengerList" title="选择乘客类型"
			:defaultIndex="passengerDefault" @cancel="passengerShow = false" @confirm="passengerConfirm"></u-picker>
		<u-picker :itemHeight="88" :immediateChange="true" :show="credentialsShow" :columns="credentialsList" title="选择证件类型"
			:defaultIndex="credentialsDefault" @cancel="credentialsShow = false" @confirm="credentialsConfirm"></u-picker>
	</view>
</template>

<script>
export default {
	name: '',
	data() {
		return {
			title: '添加乘客',
			h: uni.getSystemInfoSync().windowHeight - 87,
			mt: uni.getSystemInfoSync().statusBarHeight + 44,
			passengerName: '',
			passengerNameCopy: '',
			idcard: '',
			idcardCopy: '',
			age: '',
			passenger: '成人',
			passengerShow: false,
			passengerList: [['成人', '儿童', '免票儿童', '老人']],
			passengerDefault: [0],
			credentials: '身份证',
			credentialsShow: false,
			credentialsList: [
				['护照', '身份证', '港澳居民来往内地通行证', '台湾居民来往大陆通行证', '新版外国人永久居留身份证', '其他证件']
			],
			credentialsDefault: [1],
			credentialsIndexCfg: { 1: 1, 4: 5, 5: 0, 6: 2, 7: 3, 11: 4 },
			current: '',
			tickets: [{
				title: '长者票',
				conditions: ['年龄>=60周岁']
			},
			{
				title: '成人票',
				conditions: ['全票']
			},
			{
				title: '儿童半票',
				conditions: ['年龄<=14周岁', '身高1.2-1.5米']
			},
			{
				title: '儿童免费票',
				conditions: ['年龄<=6周岁', '身高<1.2米']
			}
			],
			show: true,
			item: null,
			isIdCard: true
		}
	},
	onLoad(option) {
		if (option.item) {
			// uni.setNavigationBarTitle({
			// 	title: '编辑乘客'
			// });
			this.title = "编辑乘客"
			this.item = JSON.parse(option.item);
			console.log('zheli', this.item);

			this.passengerName = this.item.passengerName;
			// this.passengerNameCopy = this.item.passengerNameCopy;
			this.passengerNameCopy = this.item.passengerName;
			this.credentials = this.$uct.typeToText[this.item.cardType];
			this.isIdCard = this.credentials == '身份证' ? true : false;
			this.credentialsDefault = [this.credentialsIndexCfg[this.item.cardType] || ''];
			this.idcard = this.item.card;
			// this.idcardCopy = this.item.cardCopy;
			this.idcardCopy = this.item.card;
			this.age = this.item.age;
			this.current = 1;
		}
		if (option.type && option.type == 'ship') this.show = false;
	},
	methods: {
		passengerConfirm(e) {
			this.passenger = e.value[0];
			this.passengerShow = false;
		},
		credentialsConfirm(e) {
			this.credentials = e.value[0];
			this.isIdCard = this.credentials == '身份证' ? true : false;
			this.credentialsShow = false;
		},
		nameSure(e) {
			this.passengerName = e.target.value;
			this.passengerNameCopy = this.passengerName;
		},
		idcardSure(e) {
			this.idcard = e.target.value;
			this.idcardCopy = this.idcard;
		},
		setAge(e) {
			this.age = e.target.value;
		},
		sure() {
			if (!this.passengerName) return this.$showToast('请输入乘客姓名');
			if (this.isIdCard && !this.$reg.idCard(this.idcard)) return this.$showToast('请输入正确的证件号码');
			if (!this.isIdCard && !this.idcard) return this.$showToast('请输入乘客证件号码');
			if (!this.isIdCard && !this.age) return this.$showToast('请输入乘客年龄');

			// let nowParse = Date.parse(new Date().Format('yyyy-MM-dd'));
			// let idcardNyr = this.idcard.substr(6,8);
			// let nyr = idcardNyr.substr(0,4)+'-'+idcardNyr.substr(4,2)+'-'+idcardNyr.substr(6,2);
			// if(this.idcard.length==15){
			// 	idcardNyr = this.idcard.substr(6,6);
			// 	nyr = '19'+idcardNyr.substr(0,2)+'-'+idcardNyr.substr(2,2)+'-'+idcardNyr.substr(4,2);
			// }
			// let idcardParse = Date.parse(nyr);
			// let diffYear = Math.ceil((nowParse-idcardParse)/86400/365/1000);

			// let personType = this.passengerList[0].findIndex(d => d == this.passenger);
			// if(personType==0){//成人
			// 	if(diffYear<=14||diffYear>=60) return this.$showToast('当前类型为成人(14-60周岁)，输入的身份证不匹配');
			// }else if(personType==1){//儿童
			// 	if(diffYear>14||diffYear<6) return this.$showToast('当前类型为儿童(半票，6-14周岁)，输入的身份证不匹配');
			// }else if(personType==2){//免票儿童
			// 	if(diffYear>6) return this.$showToast('当前类型为免票儿童(≤6周岁)，输入的身份证不匹配');
			// }else if(personType==3){//老人
			// 	if(diffYear<60) return this.$showToast('当前类型为老人(≥60周岁)，输入的身份证不匹配');
			// }

			let params = {
				openId: JSON.parse(uni.getStorageSync('userInfo')).openId,
				passengerName: this.$aesTm.encrypt(this.passengerName),
				cardType: this.$uct.textToType[this.credentials] || '',
				card: this.$aesTm.encrypt(this.idcard),
				age: this.age
			}

			if (this.item) {
				params.id = this.item.id;
				this.$api.put('/api/commonPerson', params).then(res => {
					if (res.data.code === 0) {
						this.getOpenerEventChannel().emit('editPassengerSuccess', '');
						uni.navigateBack();
					} else this.$showModal(res.data.msg)
				})
			} else {
				this.$api.post('/api/commonPerson', params).then(res => {
					if (res.data.code === 0) {
						this.getOpenerEventChannel().emit('addPassengerSuccess', '');
						uni.navigateBack();
					} else this.$showModal(res.data.msg)
				})
			}
		},
		toScan() {
			uni.navigateTo({
				url: '/pagesMy/commonInformation/scan'
			})
		}
	}
}
</script>

<style scoped lang="less">
.page {
	background: #F5F8FA;
	padding: 0 24rpx 20rpx;
	display: flex;
	flex-direction: column;
	align-items: center;

	.scan_add {
		display: flex;
		justify-content: center;
		padding: 20rpx 0 26rpx;

		image {
			width: 36rpx;
			height: 36rpx;
			margin: 8rpx 10rpx 0 0;
		}

		&>view {
			margin-left: 10rpx;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;

			text {
				&:first-child {
					font-size: 34rpx;
					font-family: PingFang-SC-Bold, PingFang-SC;
					font-weight: bold;
					color: #333333;
					line-height: 48rpx;
				}

				&:last-child {
					font-size: 24rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #808080;
					line-height: 33rpx;
					margin-top: 10rpx;
				}
			}
		}
	}

	.ck {
		width: calc(100% - 48rpx);
		padding: 30rpx 24rpx;

		.item {
			display: flex;
			align-items: center;

			&>view {
				&:first-child {
					font-size: 28rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #777777;
					line-height: 40rpx;
					width: 180rpx;
				}

				&:nth-child(2) {
					font-size: 30rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #111111;
					line-height: 42rpx;
					width: calc(100% - 216rpx);

					input {
						font-size: 30rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #111111;
						outline: none;
						border: none;
						width: 100%;

						&::placeholder {
							font-size: 26rpx;
							font-family: PingFangSC-Regular, PingFang SC;
							font-weight: 400;
							color: #AAAAAA;
							line-height: 37rpx;
						}
					}
				}

				&:nth-child(3) {
					width: 36rpx;
					height: 36rpx;

					image {
						width: 100%;
						height: 100%;
					}
				}
			}
		}
	}

	.yhzc {
		width: calc(100% - 48rpx);
		padding: 30rpx 24rpx;

		.item {
			display: flex;
			align-items: center;

			&>view {
				&:first-child {
					font-size: 28rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #777777;
					line-height: 40rpx;
					width: 180rpx;
				}

				&:last-child {
					text {
						font-size: 28rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #007A69;
						line-height: 40rpx;

						&:last-child {
							color: #777;
							margin-left: 20rpx;
						}
					}
				}
			}
		}

		.tickets {
			padding-top: 50rpx;
			display: flex;
			align-items: center;

			&>view {
				width: calc(25% - 13rpx);
				height: 131rpx;
				border-radius: 8rpx;
				border: 1rpx solid #EFEFEF;
				margin-left: 13rpx;
				display: flex;
				flex-direction: column;
				align-items: center;

				&:first-child {
					margin-left: 0rpx;
				}

				text {
					font-size: 20rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #AAAAAA;
					line-height: 28rpx;

					&:first-child {
						font-size: 24rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #111111;
						line-height: 33rpx;
						margin-top: 15rpx;
					}

					&:nth-child(2) {
						margin-top: 13rpx;
					}

					&:last-child {
						margin-top: 8rpx;
					}
				}
			}
		}
	}

	.hr {
		width: 100%;
		height: 1rpx;
		background: #EFEFEF;
		margin: 30rpx 0;
	}

	.to_add {
		width: 100%;
		height: 174rpx;
		position: fixed;
		bottom: 0;
		background: #fff;

		.sure {
			width: calc(100% - 40rpx);
			height: 92rpx;
			line-height: 92rpx;
			text-align: center;
			border-radius: 46rpx;
			background: #007A69;
			font-size: 32rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #FFFFFF;
			margin: 16rpx 0 0 20rpx;
		}
	}
}

.box {
	margin-top: 20rpx;
	border-radius: 16rpx;
	background: #fff;
	padding: 0 24rpx;
	width: 100%;
}

.active {
	background: #E4F4FF;
	border: 1rpx solid #007A69 !important;

	text {
		color: #007A69 !important;
	}
}
</style>
<style>
/* .u-picker__view__column__item.u-line-1{
		font-size: 30rpx !important;
		font-family: PingFangSC-Regular, PingFang SC;
		color: #111111 !important;
		height: 88rpx !important;
		line-height: 88rpx !important;
	} */
</style>